class Solution {
public:
    int candy(vector<int>& ratings) {
        vector<int> arr(ratings.size(), 1); // 每个小孩先给1个
        // 处理右侧比左侧评分高的情况
        for (int i = 1; i < ratings.size(); i++) {
            if (ratings[i] > ratings[i - 1]) {
                arr[i] = arr[i - 1] + 1;
            }
        }
        // 处理左侧比右侧评分高的情况
        for (int i = ratings.size() - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                arr[i] = max(arr[i + 1] + 1, arr[i]);
            }
        }
        int result = 0;
        for (auto x : arr) result += x;
        return result;
    }
};
